const bcryptjs = require('bcryptjs');
const joi = require('joi');
const dbQuery = require('../model/db');
const {
	sendError,
	sendSuccess
} = require('../utils/sendFunction');

// 验证规则
const schemaAdmin = joi.object({
	username: joi.string().min(4).max(12).alphanum().required().error(new Error('username不符合验证规则')),
	password: joi.string().min(4).max(12).required().error(new Error('password不符合验证规则'))
})

const schemaUser = joi.object({
	username: joi.string().min(4).max(12).alphanum().required().error(new Error('username不符合验证规则')),
	password: joi.string().min(4).max(12).required().error(new Error('password不符合验证规则')),
	nickname: joi.string().min(1).max(10).required().error(new Error('nickname不符合验证规则')),
	create_time: joi.string().required().error(new Error('create_time不符合验证规则')),
	sex: joi.string().required().error(new Error('sex不符合验证规则'))
})

// username(4-12) password(4-12)
const admin = async function (req, res, next) {
	let {
		username,
		password
	} = req.body;

	if (!username || !password) {
		sendError(res, 200, '用户名或密码不能为空!');
		return;
	}

	try {
		await schemaAdmin.validateAsync({
			username,
			password
		})
	} catch (error) {
		next(error);
		return;
	}

	let sqlStr = 'select * from admin where username=?'
	let result = await dbQuery(sqlStr, username);
	if (result.length !== 0) {
		sendError(res, 200, '用户名被占用，请更换其他用户名!');
		return;
	}

	// 加密密码
	let salt = await bcryptjs.genSalt(10);
	let hashPassword = await bcryptjs.hash(password, salt);
	let sqlStr1 = 'insert into admin (username,password) values (?,?)';
	let result1 = await dbQuery(sqlStr1, [username, hashPassword]);
	if (result1.affectedRows == 1) {
		sendSuccess(res, '注册管理员成功!');
	} else {
		sendError(res, 500, '注册管理员失败，请稍后再试!')
	}
}

// username(4-12) password(4-12) nickname(1-10) sex avator create_time 必传   introduction 不必传
const user = async function (req, res, next) {
	let info = req.body;

	if (!info.username || !info.password || !info.nickname || !info.create_time || !info.sex) {
		sendError(res, 200, '用户名或密码或昵称或创建时间或性别不能为空!');
		return;
	}

	if(!req.file){
		sendError(res,200,'头像不能为空！');
		return;
	}

	// 图片存放服务器的地址
	// console.log('localhost:3001/'+req.file.path.split('server\\')[1].replace('\\','/'));
	// console.log(req.file.path.split('server\\')[1].replace('\\', '/').replace('\\', '/'));

	try {
		await schemaUser.validateAsync({
			username: info.username,
			password: info.password,
			nickname: info.nickname,
			create_time: info.create_time,
			sex: info.sex
		})
	} catch (error) {
		next(error);
		return;
	}

	let sqlStr = 'select * from user where username=? or user_nickname=?'
	let result = await dbQuery(sqlStr, [info.username, info.nickname]);
	if (result.length !== 0) {
		sendError(res, 200, '用户名或昵称被占用，请更换其他用户名或昵称!');
		return;
	}

	let avator = req.file.path.split('server\\')[1].replace('\\', '/').replace('\\', '/');
	// 加密密码
	let salt = await bcryptjs.genSalt(10);
	let hashPassword = await bcryptjs.hash(info.password, salt);
	let sqlStr1 = 'insert into user (username,password,user_nickname,user_sex,user_avator_url,user_create_time,user_introduction) values (?,?,?,?,?,?,?)';
	let result1 = await dbQuery(sqlStr1, [info.username, hashPassword, info.nickname, info.sex, avator, info.create_time, info.introduction]);
	if (result1.affectedRows == 1) {
		sendSuccess(res, '注册用户成功!');
	} else {
		sendError(res, 500, '注册用户失败，请稍后再试!')
	}
}

module.exports = {
	admin,
	user
}